﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helper.Business.Implementation;
using Helper.Metadata;
using Helper.Infrastructure.Reporting;
using System.IO;
using Helper.Reporting.Objects.Business;
using CrystalDecisions.CrystalReports.Engine;
using Helper.Business.Repository;

namespace Helper.Controllers
{
    public partial class BusinessController : BaseController
    {
        IBusinessRepository _BusinessRepository;

        public BusinessController()
        {
            if (_BusinessRepository == null)
                _BusinessRepository = new BusinessRepository();
            
            this.LoginNeeded = true;
        }

        //
        // GET: /Provider/

        public ActionResult Index(int First = 0, int Max = 20)
        {
            return View(_BusinessRepository.GetAll(First, Max));
        }

        //
        // GET: /Provider/Details/5

        public ActionResult Details(int id)
        {
            return View(_BusinessRepository.FindById(id));
        }

        //
        // GET: /Provider/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Provider/Create

        [HttpPost]
        public ActionResult Create(Core.Entity.Business collection)
        {
            try
            {
                _BusinessRepository.Add(collection);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Provider/Edit/5
 
        public ActionResult Edit(int id)
        {
            BusinessMetadata meta = new BusinessMetadata();
            Core.Entity.Business bus = _BusinessRepository.FindById(id);
            meta.Id = id;
            meta.Name = bus.Name;            
            return View(meta);
        }

        //
        // POST: /Provider/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Core.Entity.Business collection)
        {
            try
            {
                // TODO: Add update logic here
                Core.Entity.Business bus = _BusinessRepository.FindById(id);
                bus.Name = collection.Name;
                _BusinessRepository.Save(bus);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Provider/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(_BusinessRepository.FindById(id));
        }

        //
        // POST: /Provider/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Core.Entity.Business collection)
        {
            try
            {
                // TODO: Add delete logic here         
                UserRepository _userRepo = new UserRepository();
                foreach(Core.Entity.User item in _userRepo.FindUsersByBusiness(id))
                    _userRepo.Delete(item);
                _BusinessRepository.Delete(_BusinessRepository.FindById(id));
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ViewBag.ErrorGiven = ex.Message;
              //  return View();
                return RedirectToAction("Index");
            }
        }

        public ActionResult Report(BusinessReport Report, ReportType ReturnType)
        {
            switch (Report)
            {
                case BusinessReport.ShowAll:
                    rptShowAllBusinesses rptSAB = new rptShowAllBusinesses();
                    rptSAB.SetDataSource(_BusinessRepository.GetAll());
                    Streaming str = Streaming.GetFileFromReport((ReportClass)rptSAB, ReturnType);
                    return File(str.StreamReport, str.MimeType);
                default:
                    return File(Stream.Null, "application/pdf");
            }
        }
    }
}
